package require hwt;
package require hwtk;

namespace eval ::CreateNote {
	variable m_initdir ""
	variable m_Vprecision 3
	variable m_PrecisionList {0 1 2 3 4 5 6 7 8 9 10 11 12};
	variable m_Bind "element"
	variable m_BindList "element node"
	variable m_autohide false;
	variable m_transparency false;
	variable m_eid false;
	variable m_contour true;
}

proc ::CreateNote::Import { } {	
	variable m_initdir;
	set types_r {
		{{csv Files}      {.csv}     }
		{{txt Files}      {.txt}     }
		{{All Files}        *        }
	}
	set filename [ tk_getOpenFile -defaultextension "csv" -initialdir $m_initdir -filetypes $types_r -title "Import File ..."]
	if { $filename == "" } { return 0}
	set m_initdir $filename
	if [ catch { open "$filename" r} res1 ] {
		puts "Cannot open $filename for read:$res1"
		return 0
	}

	DoImport $res1

	catch { close $res1 }
	puts "Import : $filename"
	return 1
}

proc ::CreateNote::DoImport { infile } {
	variable m_Bind
	variable headname
	variable head [list]
	variable size 0
	
	while { [gets $infile line] == 0} { continue } 	
	set line [ split $line "," ]
	set data [list]
	foreach item $line { lappend data [ string trim $item ] }
	
	set size [ llength $data ]
	if { $size < 2 } return;
	
	set headname [string trim [ lindex $data 0 ] ]
	set head [ lrange $data 2 end ]
	
	### get postIhandle
	set ui [expr rand()][clock seconds];
	hwi GetActiveClientHandle handc$ui;
	
	while { [gets $infile line] >= 0} {
		set data [ split $line "," ]
		if { [ llength $data ] < $size } { continue }
		SetNote handc $ui $data
	}

	####
	handc$ui Draw
	### release handle
	handc$ui ReleaseHandle;
	###
}

proc ::CreateNote::SetNote { handc ui data } {
	variable m_Vprecision 
	variable m_autohide
	variable m_transparency
	variable m_Bind
	variable m_eid
	variable m_contour
	
	variable headname	
	variable head	
	variable size
	
	set name  [ lindex $data 0 ]
	set id [ lindex $data 1 ]
	set value [ lrange $data 2 $size-1 ]
	
	set note "$headname: $name"
	if $m_eid {
		append note "\nEID: $id"
	}
	foreach k $head v $value {
		append note [ format "\n$k: %.${m_Vprecision}f" $v ]
	}
	if $m_contour {
		append note "\nFEA: {entity.contour_val, %0.${m_Vprecision}f}"
	}
	
	set ntid [$handc$ui AddNote];
	$handc$ui GetNoteHandle handn$ui $ntid
	handn$ui SetName $name
	handn$ui SetText $note
	handn$ui SetAttachment "$m_Bind $id"
	handn$ui SetPositionToAttachment true
	handn$ui SetTransparency $m_transparency
	handn$ui SetAutoHide $m_autohide
	handn$ui SetVisibility true
	handn$ui ReleaseHandle
}


proc ::CreateNote::ClearAllNotes { args } {

	### get postIhandle
	set ui [expr rand()][clock seconds]
	hwi GetActiveClientHandle handc$ui;
	###

	foreach id [ handc$ui GetNoteList] {
		catch {handc$ui RemoveNote $id}
	}

	handc$ui Draw
	### release handle
	handc$ui ReleaseHandle;
	###
}

proc ::CreateNote::GUI { args } {
	if {[winfo exists .create_notes]} {destroy .create_notes}
	hwtk::dialog .create_notes -title CreateNotes -transient .
	.create_notes hide apply
	.create_notes hide ok
	.create_notes hide cancel
	.create_notes post
	set recess [.create_notes recess]
	
	set c1 [ checkbutton $recess.check1 -text "Auto-hide" -anchor nw -offvalue false -onvalue true -variable [namespace current]::m_autohide ]
	set c2 [ checkbutton $recess.check2 -text "Transparency" -anchor nw -offvalue false -onvalue true -variable [namespace current]::m_transparency ]
	set c3 [ checkbutton $recess.check3 -text "EID" -anchor nw -offvalue false -onvalue true -variable [namespace current]::m_eid ]
	set c4 [ checkbutton $recess.check4 -text "FEA" -anchor nw -offvalue false -onvalue true -variable [namespace current]::m_contour ]
	
	set l1 [ hwt::AddEntry $recess.list1 \
				-label "Bind to:" \
				-labelwidth  10 \
				-textvariable [namespace current]::m_Bind\
				-listVar notyping fromPopDown [namespace current]::m_BindList\
				-state normal \
				-entryWidth 8 ]
    set l2 [ hwt::AddEntry $recess.list2 \
				-label "Precision:" \
				-labelwidth  10 \
				-textvariable [namespace current]::m_Vprecision\
				-listVar notyping fromPopDown [namespace current]::m_PrecisionList\
				-state normal \
				-entryWidth 8 ]
			
	set b1 [ button $recess.b1 -text "Create Notes" -width [ hwt::DluWidth 10 ] -command { ::CreateNote::Import } ]
	set b2 [ button $recess.b2 -text "Clear Notes" -width [ hwt::DluWidth 10 ] -command { ::CreateNote::ClearAllNotes } ]
	
	
	set width [ hwt::DluWidth 5 ]
	set height [ hwt::DluWidth 2 ]
	grid $c1 -column 0 -row 0 -sticky nw -padx $width -pady $height;
	grid $c2 -column 0 -row 1 -sticky nw -padx $width -pady $height;
	grid $c3 -column 0 -row 2 -sticky nw -padx $width -pady $height;
	grid $c4 -column 0 -row 3 -sticky nw -padx $width -pady $height;
	grid $l1 -column 1 -row 0 -sticky nw -padx $width -pady $height;
	grid $l2 -column 1 -row 1 -sticky nw -padx $width -pady $height;
	grid $b1 -column 2 -row 0 -sticky nw -padx $width -pady $height;
	grid $b2 -column 2 -row 1 -sticky nw -padx $width -pady $height;
	grid rowconfigure $recess "all" -uniform allTheSame
}

::CreateNote::GUI